package com.hu.zhcc.shiro.service;

import com.hu.zhcc.shiro.dao.OrderSettingDAO;
import com.hu.zhcc.shiro.entity.bo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */

@Service("orderSettingService")
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{
    @Autowired
    private OrderSettingDAO orderSettingDAO;

    //批量导入预约设置数据
    @Override
    public void add(List<OrderSetting> list) {
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = orderSettingDAO.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate > 0){
                    //已经进行了预约设置，执行更新操作
                    orderSettingDAO.editNumberByOrderDate(orderSetting);
                }else{
                    //没有进行预约设置，执行插入操作
                    orderSettingDAO.add(orderSetting);
                }
            }
        }

    }

    //根据月份查询对应的预约设置数据
    @Override
    public List<OrderSetting> getOrderSettingByMonth() {
        List<OrderSetting> list = orderSettingDAO.getOrderSettingByMonth();
        return list;
    }

    //根据日期设置对应的预约设置数据
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行了预约设置
        long count = orderSettingDAO.findCountByOrderDate(orderDate);
        if(count > 0){
            //当前日期已经进行了预约设置，需要执行更新操作
            orderSettingDAO.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有就那些预约设置，需要执行插入操作
            orderSettingDAO.add(orderSetting);
        }
    }

    @Override
    public OrderSetting getOrder(String date) {
        return orderSettingDAO.getOrder(date);
    }

    @Override
    public void edit(OrderSetting orderSetting) { orderSettingDAO.edit(orderSetting);
    }
}
